import logging
import time

from flask import Blueprint, request
from flask import jsonify

from airflow.patsnap.service import user_service
from airflow.models import DagPickle
from airflow.models.idata_user import User
from airflow.www.api import provide_user, exception_handle

log = logging.getLogger(__name__)

api_speech_user = Blueprint('api_speech_user', __name__)


# user route
@api_speech_user.route('/users/<int:uid>', methods=['GET'])
@exception_handle
def get_user(uid):
    user = user_service.get_user(uid)
    return jsonify(code=0, message='ok', data=User.to_json(user))


@api_speech_user.route('/users/admin', methods=['GET'])
@exception_handle
@provide_user
def get_user_admin(oa_user_name):
    is_admin = user_service.is_admin(oa_user_name)
    return jsonify(code=0, message='ok', data=is_admin)


@api_speech_user.route('/users', methods=['GET'])
@exception_handle
def get_users():
    page = request.args.get('page', 0, type=int)
    result = user_service.get_users(page)
    return jsonify(code=0, message='ok', data=result)


@api_speech_user.route('/users', methods=['POST'])
@provide_user
@exception_handle
def add_user(oa_user_name):
    user = request.json
    user_name = user['user_name']
    add_oa_user_name = user['oa_user_name']
    department_name = user['department_name']
    role_ids = user['roles']

    if user_name is None or oa_user_name is None or department_name is None:
        log.error("add_user params error user_name={%s},oa_user_name={%s},department_name={%s},",
                  user_name, add_oa_user_name, department_name)
        return jsonify(code=400, message="add_user params has None,", data=None)
    user_service.add_user(user_name, add_oa_user_name, department_name, oa_user_name, role_ids)
    return jsonify(code=0, message='ok', data=None)


@api_speech_user.route('/users/<int:id>', methods=['DELETE'])
@exception_handle
def delete_user(id):
    user = request.json
    oa_user_name = user['oa_user_name']
    if oa_user_name is None:
        log.error("delete_user params error oa_user_name={%s}", oa_user_name)
        return jsonify(code=400, message="delete_user params has None,", data=None)
    user_service.delete_user(id, oa_user_name)
    return jsonify(code=0, message='ok', data=None)


@api_speech_user.route('/users', methods=['PATCH'])
@exception_handle
@provide_user
def update_user(oa_user_name):
    user = request.json
    user_id = user.get('id', None)
    user_name = user.get('user_name', None)
    add_oa_user_name = user.get('oa_user_name', None)
    department_name = user.get('department_name', None)
    role_ids = user.get('roles', None)

    if user_id is None or role_ids is None or oa_user_name is None:
        log.error(
            "update_user params error user_id={%s},role_ids={%s},oa_user_name={%s}",
            user_id, role_ids,oa_user_name)
        return jsonify(code=400, message="update_user params has None,", data=None)
    user_service.update_user(user_id, user_name, add_oa_user_name, department_name, oa_user_name, role_ids)
    return jsonify(code=0, message='ok', data=None)


# 获取所有项目
@api_speech_user.route('/users/projects', methods=['GET'])
@exception_handle
@provide_user
def get_user_projects(oa_user_name):
    page = request.args.get('page', 0, type=int)
    result = user_service.get_user_projects(page, oa_user_name)
    return jsonify(code=0, message='ok', data=result)

if __name__ == '__main__':
    start_time_s = 1635929239
    end_time_s = 1635929587
    s_t_s = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(start_time_s))
    e_t_s = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(end_time_s))
    print(s_t_s)
    print(e_t_s)